<template>
	<view>
		<z-paging :fixed="true" ref="paging" v-model="bookList" @query="queryList">
			<view class="con_box">
				<view class="con_box_item d-b-c" v-for="(item, index) in bookList" :key="index" :class="{'con_box_item1':bookId == item.book_id}" @click="bookId=item.book_id">
					<view class="d-s-c">
						<view class="con_box_item_img">
							<image :src="item.book_img" mode=""></image>
						</view>
						<view class="ml20 mr20">
							<view class="f30 fb gray3 mb10">{{item.book_name}}</view>
							<view class="f22 gray6">单词数量{{item.total_word_num}}个,已选{{item.study_word_num}}个,剩余{{sub(item.total_word_num, item.study_word_num)}}个</view>
						</view>
					</view>
					<view v-if="bookId == item.book_id">
						<u-icon name="checkmark" color="#387BEF" size="40"></u-icon>
					</view>
				</view>
			</view>
			<view class="chapt_bot d-b-c" slot="bottom">
				<view class="chapt_bot_but1" @click="goBack">返回上层</view>
				<view class="chapt_bot_but2" @tap="confirm">确认</view>
			</view>
		</z-paging>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				imgUrl: getApp().globalData.imgUrl,
				bookId: 0,
				bookList: []
			}
		},
		computed: {
			sub(val1, val2) {
				return (val1, val2) => {
					return Number(val1) - Number(val2)
				}
			},
		},
		onLoad(option) {
			if (!this.$store.state.isLogin) {
				uni.$u.toast('登录超时请重新登录')
				setTimeout(() => {
					uni.navigateTo({
						url: '/pages/login/login',
					});
				}, 1500)
				return false
			} else {
				this.bookId = option.bookId || 0
			}
		},
		methods: {
			// 获取教材列表
			queryList(pageNo, pageSize) {
				this.$u.post(this.$u.api.userBookList, {
					pageNo: pageNo,
					pageSize: pageSize,
				}).then((res) => {
					if (res.code == 1) {
						this.$refs.paging.complete(res.data.data)
					} else {
						this.$refs.paging.complete(false)
					}
				}).catch((err) => {
					this.$refs.paging.complete(false)
				})
			},
			goBack() {
				let pages = getCurrentPages() // 当前页面
				let beforePage = pages[pages.length - 2] // 上一页
				if (beforePage) {
					uni.navigateBack({
						success: function() {
							beforePage.$vm.reload() // 执行上一页的onLoad里面的方法方法
						}
					})
				} else {
					uni.navigateTo({
						url: '/pages/index/index'
					})
				}
			},
			confirm() {
				if (this.bookId == 0) {
					uni.showToast({
						title: '请选择学习教材',
						icon: 'none'
					})
					return false
				}
				uni.showLoading({
					title: '正在提交...',
					mask: true
				})
				this.$u.post(this.$u.api.userBookCurrent, {
					bookId: this.bookId,
				}).then((res) => {
					if (res.code == 1) {
						uni.hideLoading()
						uni.showToast({
							title: '提交成功',
							icon: 'none'
						})
						setTimeout(() => {
							uni.navigateBack()
						}, 2000)
					} else {
						uni.hideLoading()
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				}).catch((err) => {
					uni.hideLoading()
					uni.showToast({
						title: err.data.msg,
						icon: 'none'
					})
				})
			},
		}
	}
</script>

<style lang="less" scoped>
	page {
		background-color: #F8F8F8;
	}
	
	.con_box{
		width: 92%;
		border-radius: 20rpx;
		padding-bottom: 20rpx;
		margin: 0 auto;
	}
	.con_box_item{
		padding: 2vh;
		margin: 20rpx auto 0;
		background: #FFFFFF;
		border-radius: 20rpx;
		border: 4rpx solid #FFFFFF;
		
		.con_box_item_img {
			width: 72rpx;
			height: 96rpx;
			
			image{
				width: 72rpx;
				height: 96rpx;
			}
		}
		
		.con_box_item_but{
			width: 140rpx;
			height: 50rpx;
			background: linear-gradient(180deg, #1F93FF, #387BEF);
			border-radius: 10rpx;
			color: #FFFFFF;
			text-align: center;
			line-height: 50rpx;
			font-size: 24rpx;
		}
		
	}
	.con_box_item1{
		border: 4rpx solid #387BEF;
	}
	.chapt_bot{
		width: 100%;
		height: 180rpx;
		padding: 40rpx 20rpx;
		box-sizing: border-box;
		background-color: #FFFFFF;
		color: #FFFFFF;
		text-align: center;
		line-height: 100rpx;
		.chapt_bot_but1{
			width: calc(50% - 40rpx);
			height: 100rpx;
			margin: 0 20rpx;
			background: linear-gradient(90deg, #999999, #C8C4C4);
			border-radius: 50rpx;
			box-sizing: border-box;
		}
		.chapt_bot_but2{
			width: calc(50% - 40rpx);
			height: 100rpx;
			margin: 0 20rpx;
			background: linear-gradient(90deg, #1F93FF, #387BEF);
			border-radius: 50rpx;
			box-sizing: border-box;
		}
	}
</style>
